package com.selen.mapper;

import com.selen.entities.EmpRole;
import com.selen.entities.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Selen
 */
public interface RoleMapper {
    @Insert("insert into  ums_role (`title`, `info`, " +
            "`deleted`, `created`, `updated`) " +
            "values (#{title}, #{info}, " +
            " #{deleted}, #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Role role);

    @Select("select ur.id, ur.title, ur.info, " +
            "ur.deleted, ur.created, ur.updated from  ums_role ur " +
            "where ur.id = #{param1} and ur.deleted = 0")
    Role selectById(Long id);

    @Update("<script>" +
            "update  ums_role " +
            "<set> " +
            "<if test='title     != null'>  title       =  #{title}    , </if> " +
            "<if test='info      != null'>  info        =  #{info}     , </if> " +
            "<if test='deleted   != null'>  deleted     =  #{deleted}  , </if> " +
            "<if test='created   != null'>  created     =  #{created}  , </if> " +
            "<if test='updated   != null'>  updated     =  #{updated}  , </if> " +
            "</set> " +
            "where id = #{id} and deleted = 0 " +
            "</script>")
    int updateById(Role role);

    @Update("update  ums_role set deleted = 1, updated = now() where id = #{param1}")
    int logicDeleteById(Long id);

    @Update("<script>" +
            "update  ums_role set deleted = 1, updated = now() where" +
            "<foreach collection = 'list' item = 'e' open = 'id in (' close = ')' separator = ','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Select("select ur.id, ur.title, ur.info, " +
            "ur.deleted, ur.created, ur.updated from  ums_role ur where ur.deleted = 0")
    List<Role> selectAll();

    @Delete("delete from  ums_role where id = #{param1}")
    int deleteById(Long id);

    @Select("select ur.id, ur.title, ur.info, ur.deleted, ur.created, ur.updated " +
            "from ums_emp_role as uer " +
            "join ums_role as ur on uer.fk_role_id = ur.id " +
            "join ums_emp as ue on uer.fk_emp_id = ue.id " +
            "where uer.fk_emp_id = #{param1} " +
            "and ur.deleted = 0 and ue.deleted = 0 and uer.deleted = 0 ")
    List<Role> selectByEmpId(Long id);

    @Update("update ums_emp_role set deleted = 1, updated = now() where fk_emp_id = #{param1}")
    int logicDeleteByEmpId(Long id);

    @Insert("insert into ums_emp_role (`fk_emp_id`, `fk_role_id`, " +
            "`deleted`, `created`, `updated`) " +
            "values (#{fkEmpId}, #{fkRoleId}, " +
            " #{deleted}, #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertEmpRole(EmpRole empRole);


}
